<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Quotation_letter extends CI_Controller {

	public function __construct()
	{
		parent::__construct();

		$this->load->database();
		$this->load->helper('url');

		$this->load->library('grocery_CRUD');
	}

	public function _example_output($output = null)
	{
		$this->load->view('master/grid',$output);
	}
	public function main()
	{
		
		$this->load->vars('title','Quotation Letter');

		$crud = new grocery_CRUD();
		
		$crud->set_theme('flexigrid');
		$this->config->load('grocery_crud');
		$crud->set_table('quotation_letter');
		$crud->set_subject('Quotation Letter');
		
		$crud->columns('no_surat','tgl_surat','ql_no','ql_date','Alamat_pemeriksaan','Tanggal_Periksa');
		$crud->display_as('no_surat','No. Surat');
		$crud->display_as('tgl_surat','Tanggal Surat');
		$crud->display_as('ql_no','No. Quotation');
		$crud->display_as('id_afiliasi','Cabang Afiliasi');
		$crud->display_as('Alamat_pemeriksaan','Tempat Pemeriksaan');
		$crud->display_as('Tanggal_Periksa','Tanggal Pemeriksaan');

		$crud->set_relation('id_afiliasi','cabang','nama');

		$crud->add_fields(
			'no_surat',
			'tgl_surat',
			'id_afiliasi',
			'Importir',
			'Alamat_Importir',
			'Telp_Importir',
			'Fax_Importir',
			'ql_attn',
			'ql_dear',
			'ql_no',
			'ql_date',
			'Alamat_Pemeriksaan','Tanggal_Periksa',
			'ql_price_curr','ql_price',
			'ql_vat','ql_total',
			'ql_charge_curr','ql_charge',
			'ql_tertanda');
		$crud->edit_fields(
			'no_surat',
			'tgl_surat',
			'id_afiliasi',
			'Importir',
			'Alamat_Importir',
			'Telp_Importir',
			'Fax_Importir',
			'ql_attn',
			'ql_dear',
			'ql_no',
			'ql_date',
			'Alamat_Pemeriksaan','Tanggal_Periksa',
			'ql_price_curr','ql_price',
			'ql_vat','ql_total',
			'ql_charge_curr','ql_charge',
			'ql_tertanda');

		$crud->display_as('ql_attn','Attn Importir');
		$crud->display_as('ql_dear','Surat Kepada (nama)');
		$crud->display_as('ql_no','No');
		$crud->display_as('ql_date','Tanggal');
		$crud->display_as('ql_total','Total');
		$crud->display_as('ql_tertanda','Tertanda');
		$crud->display_as('ql_price_curr','Inspection Fee');
		$crud->display_as('ql_price','');
		$crud->display_as('ql_vat','VAT(10%)');
		$crud->display_as('ql_charge_curr','Charge');
		$crud->display_as('ql_charge','');
		//$crud->unset_read();

		$crud->add_action('', 'detail', 'quotation_letter/pdf','ui-icon ui-icon-disk ql_pdf_cmd');
		$crud->unset_read();
		$crud->unset_export();
		$crud->unset_print();
		
		
		//$output->title = ;
		$output = $crud->render();

		$this->_example_output($output);
	}

	public function pdf($id)
	{
		$ql = $this->db->where('id',$id)->get('quotation_letter')->row();
		$config = $this->db->get('config')->row();
		if($ql){
			//// PDF STAERTED
			// As PDF creation takes a bit of memory, we're saving the created file in /downloads/reports/
			$filename = 'quotation-letter-'.$id;
			$pdfFilePath = FCPATH."/dl/pdfs/$filename.pdf";

			//die($pdfFilePath);
			$data['page_title'] = 'Hello world'; // pass data to the view
			$data['ql'] = $ql; 
			$data['config'] = $config;
			//if (file_exists($pdfFilePath) == FALSE)
			//{
			    ini_set('memory_limit','32M'); // boost the memory limit if it's low <img src="http://davidsimpson.me/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley">
			    $html = $this->load->view('pdfs/_quotation_letter', $data, true); // render the view into HTML
			     
			    $this->load->library('pdf');
			    $pdf = $this->pdf->load();
			  //  $pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); // Add a footer for good measure <img src="http://davidsimpson.me/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley">
			  
			    //$pdf->WriteHTML($css,1);
			    //$pdf->WriteHTML($html); // write the HTML into the PDF
			    //$pdf->Output($pdfFilePath, 'F'); // save to file because we can
			    //$pdf->SetDisplayMode('fullpage');

				$pdf->WriteHTML($html);

				$pdf->Output($filename.'.pdf','D');  
			//}
			 
			//redirect("/dl/pdfs/$filename.pdf"); 

			//// PDF ENDED
		}
	}

}